﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;

namespace map.member.money
{
    public partial class ChangeBankInfo : System.Web.UI.Page
    {
        protected cbuser user;
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxFunction));
            InitBaseInfo();
            if (!IsPostBack)
            {
                BankNameInput.Value = user.BankName.Value;
                BankBranchNameInput.Value = user.BankBranchName.Value;
                BankAccountTypeSelect.SelectedIndex = (user.BankAccountType.Value == RuleCode.Enum.BankAccountType.CompanyAccount) ? 1 : 0;
                BankAccountNameInput.Value = user.BankAccountName.Value;
                BankAccountInput.Value = "";// GetAccountNumber();// user.BankAccount.Value;
            }
        }

        #region 辅助方法
        private void InitBaseInfo()
        {
            user = cbuser.Find("cbuserid='" + AliApi.appUserId + "'");
        }

        protected string GetAccountNumber()
        {
            string result = string.Empty;
            if (user.Count > 0)
            {
                result = user.BankAccount.Value;
                if (result.Length > 7)
                {
                    string newResult = result;
                    result = string.Empty;
                    for (int i = 0; i < newResult.Length; i++)
                    {
                        if (i % 4 == 0 && i != 0)
                            result += " ";
                        if (i < 4 || i > newResult.Length - 4)
                            result += newResult[i].ToString();
                        else
                            result += "*";
                    }
                }
            }
            return result;
        }
        #endregion

        #region 事件
        protected void saveChange_SeverClick(object sender, EventArgs e)
        {
            if (user.Count > 0)
            {
                user.BankAccount.Value = BankAccountInput.Value;
                user.BankAccountName.Value = BankAccountNameInput.Value;
                user.BankAccountType.Value = Convert.ToInt32(BankAccountTypeSelect.Items[BankAccountTypeSelect.SelectedIndex].Value);
                user.BankBranchName.Value = BankBranchNameInput.Value;
                user.BankName.Value = BankNameInput.Value;
                user.Modify();
                if (Request["type"] == "0")
                {
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "changeBankInfo", "<script type=\"text/javascript\">refreshParentPage();</script>", false);
                }
                else
                {
                    MessageBox.Show(this.Page ,"银行账户信息修改成功");
                }
            }
        }
        #endregion
    }
}
